﻿using AnChao.WPF.Controls.CheckBoxList.Controls.Helpers;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using static AnChao.WPF.Controls.CheckBoxList.Helpers.Attributes;

namespace AnChao.WPF.Controls.CheckBoxList.Controls
{
    /// <summary>
    /// 数据字典基类
    /// </summary>
    public class DictType
    {
        public DictType() { }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id">字典ID</param>
        /// <param name="name">字典名称</param>
        public DictType(string id, string name)
        {
            Id = id;
            Name = name;
        }

        //字典ID
        public string Id { get; set; }

        //字典名称
        public string Name { get; set; }

        //字典全称
        public virtual string FullName
        {
            get
            {
                if (string.IsNullOrEmpty(Id) && string.IsNullOrEmpty(Name))
                {
                    return "";
                }
                if (string.IsNullOrEmpty(Name))
                {
                    return Id;
                }
                else if (string.IsNullOrEmpty(Id))
                {
                    return Name;
                }
                else
                {
                    return Id + "-" + Name;
                }
            }
        }

        //字符串表示对象
        public override string ToString()
        {
            return FullName;
        }
    }
    public class DictTypeDisplayName : DictType
    {
        public DictTypeDisplayName(string kv)
        {
            Id = kv;
            Name = kv;
        }

        public DictTypeDisplayName(DictType dic)
        {
            this.Id = dic.Name;
            this.Name = dic.Name;
        }
        public override string FullName
        {
            get
            {
                return Name;
            }
         }
        //public override string FullName
        //{
        //    return FullName;
        //}
       // public override string FullName => Name;
    }

   
    /// <summary>
    /// 字典类型的比较类
    /// </summary>
    public class DictTypeComparer : IEqualityComparer<DictType>
    {
        public bool Equals(DictType source, DictType dest)
        {
            return source.Id == dest.Id;
        }

        public int GetHashCode(DictType obj)
        {
            return obj.Id.GetHashCode();
        }
    }

    public class DictTypeOrderTypeDetailFlagEx : DictType
    {
        public string OrderTypeFlag { get; set; }
        public string OrderTypeDetailFlag { get; set; }
    }

    /// <summary>
    /// 用于将普通的数据实体类转换成字典数据类型
    /// </summary>
    public static class ModelBaseConvertToDictType
    {
        private static Dictionary<Type, KeyValuePair<PropertyInfo, PropertyInfo>> _dictProperties = new Dictionary<Type, KeyValuePair<PropertyInfo, PropertyInfo>>();

        public static List<DictType> ToDictList<TSource>(this IEnumerable<TSource> source)
        {
            return ToDictList<TSource>(source, EnumDictType.None);
        }

        public static List<DictType> ToDictList<TSource>(this IEnumerable<TSource> source, EnumDictType addDictType)
        {
            if (ViewHelper.IsInDesignMode)
                return new List<DictType>();

            if (source == null)
                return new List<DictType>();

            List<DictType> result = new List<DictType>();

            //例如IEnumerable<DictType>转为List<DictType>
            if (typeof(DictType).IsAssignableFrom(typeof(TSource)))
            {
                result = source.Select(x => x as DictType).Distinct(new DictTypeComparer()).ToList();

                if (addDictType == EnumDictType.Blank)
                    result.Insert(0, new DictType { Id = "", Name = "" });
                else if (addDictType == EnumDictType.All)
                    result.Insert(0, new DictType { Id = "All", Name = "全部" });
                else if (addDictType == EnumDictType.CustAll)
                    result.Insert(0, new DictType { Id = "-1", Name = "全部" });

                return result;
            }
            else
            {
                if (!_dictProperties.ContainsKey(typeof(TSource)))
                {
                    PropertyInfo idPi = typeof(TSource).GetProperties().FirstOrDefault(x => x.GetCustomAttributes(typeof(DictTypeIdAttribute), true).Count() > 0);
                    PropertyInfo namePi = typeof(TSource).GetProperties().FirstOrDefault(x => x.GetCustomAttributes(typeof(DictTypeNameAttribute), true).Count() > 0);

                    _dictProperties[typeof(TSource)] = new KeyValuePair<PropertyInfo, PropertyInfo>(idPi, namePi);
                }

                PropertyInfo piId = _dictProperties[typeof(TSource)].Key;
                PropertyInfo piName = _dictProperties[typeof(TSource)].Value;

                if (piId != null)
                {
                    if (piName != null)
                    {
                        result = source.Select(x => (x == null ? new DictType() : new DictType { Id = piId.GetValue(x, null).ToString(), Name = piName.GetValue(x, null).ToString() })).Distinct(new DictTypeComparer()).ToList();
                    }
                    else
                    {
                        result = source.Select(x => (x == null ? new DictType() : new DictType { Id = piId.GetValue(x, null).ToString(), Name = "" })).Distinct(new DictTypeComparer()).ToList();
                    }
                }
            }

            if (addDictType == EnumDictType.Blank)
                result.Insert(0, new DictType { Id = "", Name = "" });
            else if (addDictType == EnumDictType.All)
                result.Insert(0, new DictType { Id = "All", Name = "全部" });
            else if (addDictType == EnumDictType.CustAll)
                result.Insert(0, new DictType { Id = "-1", Name = "全部" });

            return result;
        }

        
    }

    public enum EnumDictType
    {
        /// <summary>
        /// 无
        /// </summary>
        None,
        /// <summary>
        /// 空
        /// </summary>
        Blank,
        /// <summary>
        /// ALL-全部
        /// </summary>
        All,
        /// <summary>
        /// -1-全部
        /// </summary>
        CustAll,
    }
}
